<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.type" placeholder="状态" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in typeOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加礼物
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template slot-scope="{row}">
          <el-image :src="row.img" :preview-src-list="[row.img]" style="width: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="金币" align="center">
        <template slot-scope="{row}">
          <span>{{ row.coins }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" align="center" width="90">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status | statusText }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="类型" class-name="status-col" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{ row.type | typeText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否通知" class-name="status-col" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{ row.is_notice | noticeText }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" top="0" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="margin: 0 45px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="temp.title" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <Upload v-model="temp.img" :value="temp.img" />
        </el-form-item>
        <el-form-item label="SVG" prop="svg">
          <Upload v-model="temp.svg" :value="temp.svg" />
        </el-form-item>
        <el-form-item label="金币数" prop="coins">
          <el-input v-model="temp.coins" />
        </el-form-item>
        <el-form-item label="通知" prop="is_notice">
          <el-radio-group v-model="temp.is_notice">
            <el-radio v-for="status in noticeOptions" :label="status.key" border>
              {{ status.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="temp.status">
            <el-radio v-for="status in statusOptions" :label="status.key" border>
              {{ status.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="temp.type">
            <el-radio v-for="status in typeOptions" :label="status.key" border>
              {{ status.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { giftList, giftCreate, giftUpdate } from '@/api/gift'
import Upload from '@/components/Upload/SingleImage2.vue'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination, Upload },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success'
      }
      return statusMap[parseInt(status)]
    },
    statusText(status) {
      const statusMap = {
        0: '禁用',
        1: '正常'
      }
      return statusMap[parseInt(status)]
    },
    typeText(type) {
      const typeMap = {
        1: '心动礼物',
        2: '亲密礼物'
      }
      return typeMap[parseInt(type)]
    },
    noticeText(notice) {
      const noticeMap = {
        0: '不通知',
        1: '通知'
      }
      return noticeMap[parseInt(notice)]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        type: 1,
        sort: 'desc',
        sort_field: 'id'
      },
      typeOptions: [{ label: '心动礼物', key: 1 }, { label: '亲密度礼物', key: 2 }],
      noticeOptions: [{ label: '通知', key: 1 }, { label: '不通知', key: 0 }],
      statusOptions: [{ label: '显示', key: 1 }, { label: '不显示', key: 0 }],
      showReviewer: false,
      temp: {
        id: undefined,
        title: '',
        img: '',
        svg: '',
        coins: '',
        is_notice: 1,
        status: 1,
        type: 1
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        title: [{ required: true, message: '请输入名称', trigger: 'change' }],
        img: [{ required: true, message: '请上传图片', trigger: 'change' }],
        svg: [{ required: true, message: '请上传SVG', trigger: 'change' }],
        coins: [{ required: true, message: '请输入金币数', trigger: 'change' }],
        is_notice: [{ required: true, message: '请输入任务触发次数', trigger: 'change' }],
        type: [{ required: true, message: '请输入任务识别码', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      giftList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        title: '',
        description: '',
        icon: '',
        gift_id: '',
        gift_num: 1,
        task_code: '',
        task_num: 1,
        status: 1
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          giftCreate(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '成功创建任务',
              type: 'success',
              duration: 1500
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          giftUpdate(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '成功编辑任务',
              type: 'success',
              duration: 1500
            })
            this.getList()
          })
        }
      })
    }
  }
}
</script>
